package Main;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.mysql.jdbc.Statement;

import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JTextArea;

public class CarFrame extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CarFrame frame = new CarFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CarFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 420);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		textField = new JTextField();
		textField.setBounds(33, 10, 369, 32);
		contentPane.add(textField);
		textField.setColumns(10);
		JTextArea textArea = new JTextArea();
		textArea.setBounds(10, 58, 424, 110);
		contentPane.add(textArea);
		JButton btnNewButton = new JButton("查看购物车商品");
		btnNewButton.addMouseListener(new MouseAdapter() {
				String url = "jdbc:mysql://localhost:3306/goods";
			    String userName = "root";
			    String password = "kamisama";
			    String driverName = "com.mysql.jdbc.Driver";
			    String strSql = "select * from cars";
			    Scanner sc = new Scanner(System.in);
			    	Connection con = null;
			    	Statement pstmt = null;
			    	ResultSet rs = null;
			    	String driverName1 = "com.mysql.jdbc.Driver";
				@Override
				public void mouseClicked(MouseEvent e) {
					try {
						Class.forName(driverName);
				        con = (Connection) DriverManager.getConnection(url, userName, password);
				        pstmt = (Statement) con.createStatement(); 
				        rs = pstmt.executeQuery(strSql);
				        while (rs.next()) {
				            int id = rs.getInt("id");
				            String name = rs.getString("name");
				            Double price = rs.getDouble("price");
				            int num = rs.getInt("num");
				            String description = rs.getString("description");
				            double sum = rs.getDouble("sum");
				            System.out.print("id=" + id + "  name="+name+ " price="+price+"  num=" + num+" description="+description+"  sum="+sum+"\n");
				            String str = "id=" + id + "  name="+name+ " price="+price+"  num=" + num+" description="+description+"  sum="+sum+"\n";
							textArea.append(str);
				        }
					} catch (ClassNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}catch (SQLException e1){
						e1.printStackTrace();
					}finally {
			            if (rs != null)
			                try {
			                    rs.close();
			                } catch (Exception e1) {
			                    e1.printStackTrace();
			                }
			            if (pstmt != null){
			                try {
			                    pstmt.close();// 关闭语句
			                } catch (Exception e1) {
			                    e1.printStackTrace();
			                }
			            }
			            if (con != null) {
			                try {
			                    con.close();// 关闭连接
			                } catch (Exception e1) {
			                    e1.printStackTrace();
			                }
			            }
					}
				}
		});
		btnNewButton.setBounds(10, 178, 170, 32);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("返回");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Fframe f = new Fframe();
				f.setVisible(true);
				dispose();
			}
		});
		btnNewButton_1.setBounds(227, 178, 170, 32);
		contentPane.add(btnNewButton_1);
		
		JLabel lblNewLabel = new JLabel("请输入您要删除的购物车商品id");
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		lblNewLabel.setForeground(Color.BLUE);
		lblNewLabel.setBounds(20, 220, 214, 31);
		contentPane.add(lblNewLabel);
		
		textField_1 = new JTextField();
		JTextField textField_1 = new JTextField();
		String value = textField_1.getText().trim();
		textField_1.setBounds(237, 220, 85, 31);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		
		JButton btnNewButton_2 = new JButton("确认删除");
		DeleteCarGoods d = new DeleteCarGoods();
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				d.DeleteCargoods(Integer.parseInt(textField_1.getText().trim()));
			}
		});
		btnNewButton_2.setBounds(331, 221, 93, 31);
		contentPane.add(btnNewButton_2);
		
		JLabel lblid = new JLabel("请输入您要修改数量的购物车商品id");
		lblid.setForeground(Color.BLUE);
		lblid.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		lblid.setBounds(10, 285, 233, 31);
		contentPane.add(lblid);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(237, 286, 85, 31);
		contentPane.add(textField_2);
		
		JLabel label = new JLabel("请输入您要修改数量的购物车商品数量");
		label.setForeground(Color.BLUE);
		label.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		label.setBounds(10, 340, 244, 31);
		contentPane.add(label);
		
		textField_3 = new JTextField();
		textField_3.setColumns(10);
		textField_3.setBounds(251, 346, 85, 31);
		contentPane.add(textField_3);
		

		UpdataNum u = new UpdataNum();
		JButton button_1 = new JButton("确认");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String value1 = textField_2.getText().trim();
				String value2 = textField_3.getText().trim();
				u.updata(Integer.parseInt(value1), Integer.parseInt(value2));
			}
		});
		button_1.setBounds(341, 345, 93, 31);
		contentPane.add(button_1);
		

	}

}
